﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.IO;
using System.Drawing;

namespace Utility
{
    public class Folder
    {
        //目录名成
        private string _name;
        //表示是否把压缩图载入内存
        private bool _isLoaded;
        //图像存放路径
        private string _sourcePath;
       
        //用于存放一个目录下的所有图片的缩略图
        public Hashtable bmps;

        public Folder(string exePath, string name)
        {
            _name = name;
            _isLoaded = false;
            _sourcePath=exePath+"\\"+name;
            //如果文件夹不存在就创建
            if (!Directory.Exists(_sourcePath))
                Directory.CreateDirectory(_sourcePath);
        }
        //判断是否已经载入过缩略图
        public bool IsLoaded { get { return _isLoaded; } }
        //目录名称
        public string Name { get { return _name; } }
        //获取图片所在路径
        public string GetSourcePath() { return _sourcePath; }
        /// <summary>
        /// 通过原始文件名获得对应的缩略图
        /// </summary>
        /// <param name="sourcePath">原始图片名称</param>
        /// <returns>缩略图</returns>
        public Bitmap GetThunbnail(string sourcePath)
        {
            //通过原文件名获得相应的缩略图
            return (Bitmap)bmps[sourcePath];
        }
        //通过图像名称获取相应的图像
        public Bitmap GetImage(string aName)
        {
            Bitmap bmp = new Bitmap(_sourcePath+"\\"+aName);
            return bmp;
        }
        //把缩略图载入内存
        public void LoadImage()
        {
            //这里保证bmps在用到的时候才创建
            if (bmps == null)
                bmps = new Hashtable();
            foreach (string sourceFile in Directory.GetFiles(_sourcePath))
            {
                if (!PicInfo.IsImage(sourceFile))
                    continue;
                //缩略图名称
                string picName = Path.GetFileNameWithoutExtension(sourceFile) + ".bmp";
                //缩略图全部路径
                string thumbnailFile = _sourcePath + "\\" + picName;
                bmps.Add(Path.GetFileNameWithoutExtension(sourceFile), new Bitmap(thumbnailFile));
            }
            _isLoaded = true;
        }
        //在指定目录中添加一幅图片
        public void Add(string aName)
        {
            string picName = Path.GetFileNameWithoutExtension(aName) + ".bmp";
            string sourceFile = _sourcePath + "\\" + aName;
        }
        //删除一张图片
        public void Remove(string aName)
        {
            string picName = Path.GetFileNameWithoutExtension(aName) + ".bmp";
            string souceFile = _sourcePath + "\\" + aName;
            //必须先把图像从内存中释放掉才能删除图像
            try
            {
                Bitmap bmp = (Bitmap)bmps[aName];//获取图片的引用
                bmps.Remove(aName);//移出hashtable
                bmp.Dispose();//从内存释放
                File.Delete(souceFile);//删除
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        private bool ThumbnaiCallBack()
        {
            return false;
        }
        public static Rectangle GetRectFromBounds(int width, int height, Rectangle Bounds)
        {
            int x, y;
            x = Bounds.X + (Bounds.Width - width) / 2;
            y = Bounds.Y + (100 - height) / 2 + 4;
            return new Rectangle(x, y, width, height);
        }
        public static Rectangle GetRectFromBounds(Bitmap bmp, Rectangle Bounds)
        {
            int x, y;
            x = Bounds.X + (Bounds.Width - bmp.Width) / 2;
            y = Bounds.Y + (100 - bmp.Height) / 2 + 4;
            return new Rectangle(x, y, bmp.Width, bmp.Height);
        }
        //创建缩略图
        private void CreateThumbnail(string source, string dest)
        {
            //source表示原图路径，dest参数表示缩略图路径
            Image.GetThumbnailImageAbort myCallBack = new Image.GetThumbnailImageAbort(ThumbnaiCallBack);
            Bitmap bmp = new Bitmap(source);
            int x = bmp.Width;
            int y = bmp.Height;
            try
            {
                if (x > 32 || y > 32)
                {
                    float scale = (x > y) ? (x / 100F) : (y / 100F);
                    Image aThumbnail = bmp.GetThumbnailImage((int)(x / scale), (int)(x / scale), myCallBack, IntPtr.Zero);
                    aThumbnail.Save(dest);
                }
                else
                {
                    bmp.Save(dest);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                bmp.Dispose();
            }
        }
        //在制定边框中计算指定图像所绘制的矩形
        public static Rectangle GeRectFromBound(Bitmap bmp, Rectangle bounds)
        {
            int x, y;
            x = bounds.X + (bounds.Width - bmp.Width) / 2;
            y = bounds.Y + (100 - bmp.Height) / 2 + 4;
            return new Rectangle(x, y, bmp.Width, bmp.Height);
        }
        public override string ToString()
        {
            return _name;
        }
    }
}
